//
//  MineHomeViewController.m
//  B2C
//
//  Created by 李小龙 on 2017/7/18.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "MineHomeViewController.h"
#import "LoginViewController.h"
#import "MineHomeTableViewCell.h"
#import "PersonHomeViewController.h"
#import "MineOrderViewController.h"
#import "MineCollectViewController.h"
#import "MineMessageViewController.h"
#import "SettingHomeViewController.h"
#import "InvitationViewController.h"
#import "AddressManageViewController.h"
#import "OrderStatusModel.h"
#import "MineRefundListViewController.h"

@interface MineHomeViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *tableViewHeader;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *headerImageView;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) OrderStatusModel *statusModel;

@end

@implementation MineHomeViewController
#pragma mark - Get
- (OrderStatusModel *)statusModel {
    if (!_statusModel) {
        _statusModel = [[OrderStatusModel alloc] init];
    }
    return _statusModel;
}
#pragma mark - UIViewController
- (instancetype)init {
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.title = @"我的";
    self.navigationItem.leftBarButtonItem = nil;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableViewHeaderTapAction:)];
    [self.tableViewHeader addGestureRecognizer:tap];
    
    // tableView 偏移20/64适配
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [self setNavigationBarAlpha:0.0 animation:NO];
    [self.navigationController setNavigationBarHidden:YES animated:NO];

    [self createTableHeader];
    if ([UserInfo shareInstance].isLogin) {
        [self getNetData];
    } else {
        [self.statusModel updateDataZore];
        [self.tableView reloadData];
    }
    [self setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
//    [self setNavigationBarAlpha:0.0 animation:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

#pragma mark -
- (void)createTableHeader {
    if (TextIsNotNil([UserInfo shareInstance].user.mnickname)) {
        [self.nameLabel setText:[UserInfo shareInstance].user.mnickname];
    } else {
        [self.nameLabel setText:@"未设置"];
    }
    [self.headerImageView sd_setImageWithURL:GetUrl([UserInfo shareInstance].user.mimg) placeholderImage:PlaceholderImageHeader];
    [self.headerImageView setCircleWithColor:[UIColor yellowTextColor] borderWidth:2 cornerRadius:self.headerImageView.width/2.0];
    
}
- (void)tableViewHeaderTapAction:(UIGestureRecognizer *)tap {
    if ([self isLogin]) {
        PersonHomeViewController *personHomeVC = [[PersonHomeViewController alloc] init];
        [self.navigationController pushViewController:personHomeVC animated:YES];
    }
}
- (BOOL)isLogin {
    if (![UserInfo shareInstance].isLogin) {
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        BaseNavigationViewController *nav = [[BaseNavigationViewController alloc] initWithRootViewController:loginVC];
        loginVC.loginSuccess = ^{
            NSLog(@"登录成功");
        };
        [self presentViewController:nav animated:YES completion:nil];
//        LoginViewController *loginVC = [[LoginViewController alloc] init];
//        loginVC.loginSuccess = ^{
//            NSLog(@"登录成功");
//        };
//        [self.navigationController pushViewController:loginVC animated:YES];
//        //        [self presentViewController:loginVC animated:YES completion:nil];
        return NO;
    }
    return YES;
}
#pragma mark - 网络请求
- (void)getNetData {
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    [MYAFNetworking POST:URL_Get_Order_Status parameters:nil success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSString *code = responseObject[@"code"];
        if ([code isEqualToString:@"200"]) {
            self.statusModel = [OrderStatusModel mj_objectWithKeyValues:responseObject[@"data"]];
            [self.tableView reloadData];
        } else if (![code isEqualToString:@"204"]) {
            [MBProgressHUD showError:self.view title:responseObject[@"message"] code:code animated:YES];
        } else {
            [[UserInfo shareInstance] logout];
            
            [self createTableHeader];
            [self.statusModel updateDataZore];
            [self.tableView reloadData];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:self.view title:@"" code:@"" animated:YES];
    }];
}
#pragma mark - tableViewDataSoure
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 1;
        case 1:
            return 5;
        default:
            return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineHomeTableViewCell *cell = [MineHomeTableViewCell mineHomeCellWithTableView:tableView indexPath:indexPath];
    switch (indexPath.section) {
        case 0:
        {
            [cell.redPointObligation.titleLabel setText:@"待付款"];
            [cell.redPointObligation.imageView setImage:[UIImage imageNamed:@"payment"]];
            [cell.redPointObligation setRedPointText:self.statusModel.status0];
            
            [cell.redPointReceived.titleLabel setText:@"待收货"];
            [cell.redPointReceived.imageView setImage:[UIImage imageNamed:@"receipt"]];
            [cell.redPointReceived setRedPointText:self.statusModel.status2];
            
//            if (TextIsNotNil(self.statusModel.status2) && ![self.statusModel.status2 isEqualToString:@"0"]) {
//                [cell.redPointReceived.redLabel setHidden:NO];
//                [cell.redPointReceived.redLabel setText:self.statusModel.status2];
//            } else {
//                [cell.redPointReceived.redLabel setHidden:YES];
//            }
            
            [cell.redPointEvaluation.titleLabel setText:@"待评价"];
            [cell.redPointEvaluation.imageView setImage:[UIImage imageNamed:@"evaluate"]];
            [cell.redPointEvaluation setRedPointText:self.statusModel.status3];
//            if (TextIsNotNil(self.statusModel.status3) && ![self.statusModel.status3 isEqualToString:@"0"]) {
//                [cell.redPointEvaluation.redLabel setHidden:NO];
//                [cell.redPointEvaluation.redLabel setText:self.statusModel.status3];
//            } else {
//                [cell.redPointEvaluation.redLabel setHidden:YES];
//            }
            
            [cell.redPointRefunded.titleLabel setText:@"退款订单"];
            [cell.redPointRefunded.imageView setImage:[UIImage imageNamed:@"refund"]];
            [cell.redPointRefunded setRedPointText:self.statusModel.status7];
//            if (TextIsNotNil(self.statusModel.status7) && ![self.statusModel.status7 isEqualToString:@"0"]) {
//                [cell.redPointRefunded.redLabel setHidden:NO];
//                [cell.redPointRefunded.redLabel setText:self.statusModel.status7];
//            } else {
//                [cell.redPointRefunded.redLabel setHidden:YES];
//            }
            
            cell.redBlock = ^(NSInteger tag) {
                if ([self isLogin]) {
                    switch (tag) {
                        case 1000:
                        {
                            MineOrderViewController *orderVC = [[MineOrderViewController alloc] init];
                            orderVC.type = MineOrderTypeObligation;
                            [self.navigationController pushViewController:orderVC animated:YES];
                            NSLog(@"待付款");
                            break;
                        }
                        case 1001:
                        {
                            MineOrderViewController *orderVC = [[MineOrderViewController alloc] init];
                            orderVC.type = MineOrderTypeReceived;
                            [self.navigationController pushViewController:orderVC animated:YES];
                            NSLog(@"待收货");
                            break;
                        }
                        case 1002:
                        {
                            MineOrderViewController *orderVC = [[MineOrderViewController alloc] init];
                            orderVC.type = MineOrderTypeEvaluation;
                            [self.navigationController pushViewController:orderVC animated:YES];
                            NSLog(@"待评价");
                            break;
                        }
                        case 1003:
                        {
                            MineRefundListViewController *refundListVC = [[MineRefundListViewController alloc] init];
                            [self.navigationController pushViewController:refundListVC animated:YES];
                            NSLog(@"退款订单");
                            break;
                        }
                        default:
                            break;
                    }
                }
                
            };
            cell.block = ^{
                if ([self isLogin]) {
                    MineOrderViewController *orderVC = [[MineOrderViewController alloc] init];
                    orderVC.type = MineOrderTypeAll;
                    [self.navigationController pushViewController:orderVC animated:YES];
                    NSLog(@"全部订单");
                }
            };
            
            break;
        }
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                {
                    [cell.secondImageView setImage:[UIImage imageNamed:@"shoucang"]];
                    [cell.secondTitleLabel setText:@"我的收藏"];
                    break;
                }
                case 1:
                {
                    [cell.secondImageView setImage:[UIImage imageNamed:@"dizhi"]];
                    [cell.secondTitleLabel setText:@"地址管理"];
                    break;
                }
                case 2:
                {
                    [cell.secondImageView setImage:[UIImage imageNamed:@"xiaoxi"]];
                    [cell.secondTitleLabel setText:@"我的消息"];
                    break;
                }
                case 3:
                {
                    [cell.secondImageView setImage:[UIImage imageNamed:@"shezhi"]];
                    [cell.secondTitleLabel setText:@"系统设置"];
                    break;
                }
                case 4:
                {
                    [cell.secondImageView setImage:[UIImage imageNamed:@"yaoyue"]];
                    [cell.secondTitleLabel setText:@"新人邀约"];
                    break;
                }
                default:
                    break;
            }
            break;
        }
        default:
            break;
    }
    return cell;
}

#pragma mark - tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            return 134;
        case 1:
            return 60;
        default:
            return 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 5;
        case 1:
            return 0.001;
            
        default:
            return 0.001;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, 5)];
        [view setBackgroundColor:[UIColor clearColor]];
        return view;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        if ([self isLogin]) {
            switch (indexPath.row) {
                case 0:
                {
                    MineCollectViewController *collectVC = [[MineCollectViewController alloc] init];
                    [self.navigationController pushViewController:collectVC animated:YES];
                    break;
                }
                case 1:
                {
                    AddressManageViewController *manageVC = [[AddressManageViewController alloc] init];
                    [self.navigationController pushViewController:manageVC animated:YES];
                    break;
                }
                case 2:
                {
                    MineMessageViewController *messageVC = [[MineMessageViewController alloc] init];
                    [self.navigationController pushViewController:messageVC animated:YES];
                    break;
                }
                case 3:
                {
                    SettingHomeViewController *settingVC = [[SettingHomeViewController alloc] init];
                    [self.navigationController pushViewController:settingVC animated:YES];
                    break;
                }
                case 4:
                {
                    InvitationViewController *invitationVC = [[InvitationViewController alloc] init];
                    [self.navigationController pushViewController:invitationVC animated:YES];
                    break;
                }
                default:
                    break;
            }
        }
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
